package com.zxp.server.controller;


import com.wf.captcha.SpecCaptcha;
import com.zxp.server.config.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Api(value = "生成验证码", tags = "生成验证码")
@RestController
public class captchaController {

    @ApiOperation(value = "生成验证码")
    @GetMapping("/getCaptcha")
    public Result getCaptcha(HttpServletRequest request, HttpServletResponse response){
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String code = specCaptcha.text().toLowerCase();
        //redisUtil.set(key, code, 600);
        request.getSession().setAttribute("captcha", code);  //存入session
        // 将key和base64返回给前端
        Map captchaObj = new HashMap();
        captchaObj.put("code",code);
        captchaObj.put("picture",specCaptcha.toBase64());
        return Result.success("生成验证码成功",captchaObj);
    }


}
